package com.example.shop.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.shop.mapper.DepartmentMapper;
import com.example.shop.mapper.UserDepartmentMapper;
import com.example.shop.mapper.UserEducationMapper;
import com.example.shop.mapper.UserMapper;
import com.example.shop.pojo.Department;
import com.example.shop.pojo.DepartmentDto;
import com.example.shop.pojo.User;
import com.example.shop.pojo.UserDepartment;
import com.example.shop.pojo.UserDto;
import com.example.shop.service.DepartmentService;

@Service
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	private DepartmentMapper departmentMapper;
	@Autowired
	private UserDepartmentMapper userDepartmentMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserEducationMapper userEducationMapper;

	
	/**
	 * 添加部门
	 */
	@Override
	public String add(DepartmentDto departmentDto) {
		if (departmentDto.getDepartmentName() == null || departmentDto.getUserAccount() == null) {
			return "请先完善部门信息";
		}
		// 添加部门
		departmentMapper.add(departmentDto);
		System.out.print(departmentDto.toString());
		// 数据处理
		UserDepartment userDepartment = new UserDepartment();
		BeanUtils.copyProperties(departmentDto, userDepartment);
		// userDepartment.setDepartmentId(departmentDto.getDepartmentId());
		// 添加部门管理人员
		userDepartmentMapper.add(userDepartment);
		return "部门添加成功";
	}

	/**
	 * 删除部门
	 */
	public String delete(Department department) {
		if (department == null || department.getDepartmentId() == null) {
			return "必须填写部门信息";
		}

		// 删除部门
		if (departmentMapper.deleteByDepartmentId(department) > 0 &&
		// 删除部员
				userDepartmentMapper.deleteByDepartmentId(department.getDepartmentId()) > 0) {
			return "部门删除成功";
		}

		return "部门删除失败";
	}

	/**
	 * 查询部门：查询同级，同上司的部门基本信息
	 */
	@Override
	public List<DepartmentDto> selectBaseInfo(DepartmentDto departmentDto) {
		String i ="";
		//查询同上司的部门id
		List<Long> departmentIds= userDepartmentMapper.selectIsUPA(departmentDto.getUserAccount());
		//根据部门id查询同级的部门基本信息
		List<DepartmentDto> departmentBI = departmentMapper.selectBaseInfo(departmentDto.getDepartmentFatherId(),departmentIds);
		//根据账号查询上司姓名
		User user = userMapper.selectByAccount(departmentDto.getUserAccount());
		//将上司姓名放入部门基础信息中
		departmentBI.stream().forEach((s) ->{
			s.setUserParentName(user.getUserName());
		});
		//返回部门基础信息：DepartmentDto
		return departmentBI;
		
	}

	/**
	 * 查询子部门信息
	 */
	@Override
	public List<Department> selectSDP(DepartmentDto department) {
		ArrayList<Department> departments = new ArrayList<>();
		List<Department> sonDepartments = getSonDepartment(department,departments);
		return sonDepartments;
	}
	/**
	 * 获取所有子部们并处理
	 * @return
	 */
	private List<Department> getSonDepartment(Department department,List<Department> departments){
		//退出条件
		if(department == null) {
			return null;
		}
		List<Department> sonDepartments = departmentMapper.selectByDepartmentFatherId(department);
		for(Department sonDepartment:sonDepartments) {
			departments.add(sonDepartment);
			getSonDepartment(sonDepartment,departments);
		}
		return departments;
	}

	/**
	 * 查询子部门人员基本信息
	 */
	@Override
	public List<UserDto> selectSSPI(DepartmentDto department) {
		ArrayList<UserDto> departments = new ArrayList<>();
		List<UserDto>  sonDepartmentPeoples = getSonPeople(department,departments);
		return sonDepartmentPeoples;
	}
	/**
	 *
	 * 获取所有子部们人员并处理
	 * @return
	 */
	private List<UserDto> getSonPeople(Department department,List<UserDto> departments) {
		//退出条件
		if(department == null) {
			return null;
		}
		//获取所有子部门
		List<Department> sonDepartments = departmentMapper.selectByDepartmentFatherId(department);
		for(Department sonDepartment:sonDepartments) {
			//获取该部门所有成员
			List<UserDepartment> sonUserDepartments = userDepartmentMapper.selectByDepartmentId(sonDepartment.getDepartmentId());
			//获取所有部门成员信息并处理
			for(UserDepartment sonUserDepartment:sonUserDepartments) {
				UserDto user = userMapper.selectByAccount(sonUserDepartment.getUserAccount());
				departments.add(user);
			}
			getSonPeople(sonDepartment,departments);
		}
		return departments;
	
	}

	/**
	 * 更新部员
	 */
	@Override
	public String updateUserDepartment(UserDepartment userDepartment) {
		if(userDepartment.getUserAccount()==null) {
			return "请输入账号";
		}
		int i = userDepartmentMapper.updateByAccount(userDepartment);
		if(i<=0) {
			return "部员更新失败";
		}
		return "部员更新成功";
	}

	/**
	 * 查询部员
	 */
	@Override
	public UserDepartment selectUserDepartment(UserDepartment userDepartment) {
		if(userDepartment.getUserAccount() == null) {
			return null;
		}
		UserDepartment ud= userDepartmentMapper.selectByUserAccount(userDepartment);
		return ud;
	}
	
	

}
